<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\Service\ShuDu;

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
            [
                'class' => 'yii\filters\PageCache',
                'only' => ['says'],
                'duration' => 300,
//                'variations' => [
//                    \Yii::$app->language,
//                ]
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * Login action.
     *
     * @return Response|string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }

        $model->password = '';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }

    /**
     * say
     *
     * @return string
     */
    public function actionSays()
    {
//        $shudu = new ShuDu();
//        $generatedSudoku = $shudu->generateSudoku(9, 0, 4);
//        print_r(json_encode($generatedSudoku));
//        foreach ($generatedSudoku as $row) {
//            echo implode(' ', $row) . "\n";
//        }
        var_dump(time());
        return $this->render('say', ['message' => 'aaaaaa']);
    }


    /**
     * valid
     */
    public function actionValidShudu()
    {
        $params = $this->request->get();

        $shudu = new ShuDu();

        $arr = [[3, 6, 7, 2, 4, 5, 1, 8, 9], [6, 8, 5, 9, 7, 3, 2, 4, 1], [7, 4, 2, 5, 6, 1, 8, 9, 3], [8, 5, 1, 3, 9, 7, 4, 6, 2], [1, 7, 6, 8, 3, 4, 9, 2, 5], [5, 1, 3, 4, 2, 9, 6, 7, 8], [4, 3, 9, 6, 8, 2, 5, 1, 7], [2, 9, 4, 1, 5, 8, 7, 3, 6], [9, 2, 8, 7, 1, 6, 3, 5, 4]];

        $rel = $shudu->isValidSudoku($arr, 9, 4);

        print_r($rel);
    }
}
